//
//  GDRightCell.m
//  美团试验 (自己)
//
//  Created by 热带雨林 on 17/2/13.
//  Copyright © 2017年 热带雨林. All rights reserved.
//

#import "GDRightCell.h"
#import "CZAdditions.h"
#import "Masonry.h"
#import "UIImageView+WebCache.h"
#import "GDMinusView.h"

@interface GDRightCell()<GDMinusViewDelegate>

//有点迷了，将控件当成了NSString 使用了copy
@property(nonatomic, weak)UIImageView *icon;
@property(nonatomic, weak)UILabel *name;
@property(nonatomic, weak)UILabel *sale;
@property(nonatomic, weak)UIImageView *imageV;
@property(nonatomic, weak)UILabel *prise;
@property(nonatomic, weak)UILabel *price;
@property(nonatomic, weak)UILabel *desLabel;


@end

@implementation GDRightCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        [self setupUI];
    }
    return self;
}
- (void)setupUI{

    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    //图像
    UIImageView *icon = [[UIImageView alloc]init];
    icon.backgroundColor = [UIColor redColor];
    //裁圆角 第二句必须要加上
    icon.layer.cornerRadius = 10;
    icon.layer.masksToBounds = YES;
    self.icon = icon;
    [self.contentView addSubview:icon];
    
    //菜名
    UILabel *name = [UILabel cz_labelWithText:@"好好吃" fontSize:13 color:[UIColor blackColor]];
    self.name = name;
    [self.contentView addSubview:name];
    
    //月售
    UILabel *sale = [UILabel cz_labelWithText:@"889" fontSize:11 color:[UIColor cz_colorWithHex:0x7e7e7e]];
    self.sale = sale;
    [self.contentView addSubview:sale];
    
    //点赞图片
    UIImage *imageName = [UIImage imageNamed:@"icon_food_review_praise"];
    UIImageView *imageV = [[UIImageView alloc]init];
    imageV.image = imageName;
    self.imageV = imageV;
    [self.contentView addSubview:imageV];
    
    //点赞数
    UILabel *prise = [UILabel cz_labelWithText:@"100" fontSize:11 color:[UIColor cz_colorWithHex:0x7e7e7e]];
    self.prise = prise;
    [self.contentView addSubview:prise];
    
    //价格
    UILabel *price = [UILabel cz_labelWithText:@"12" fontSize:14 color:[UIColor blackColor]];
    self.price = price;
    [self.contentView addSubview:price];
    
    //描述
    UILabel *des = [UILabel cz_labelWithText:@"好好吃西啊，没事大家可以试一下，简单粗暴的生活的说,好好吃西啊，没事大家可以试一下，简单粗暴的生活的说" fontSize:11 color:[UIColor cz_colorWithHex:0x7e7e7e]];
    des.numberOfLines = 0;
    self.desLabel = des;
    [self.contentView addSubview:des];
    
    GDMinusView *minusView = [GDMinusView minusView];
    minusView.delegate = self;
    [self.contentView addSubview:minusView];
    

    [icon mas_makeConstraints:^(MASConstraintMaker *make) {
    
        make.width.height.mas_equalTo(50);
        
        make.top.equalTo(self.contentView).offset(10);
        
        make.left.equalTo(self.contentView).offset(3);
        
    }];
    
    [name mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(icon.mas_top);
        make.left.equalTo(icon.mas_right).offset(10);
    }];
    
    [sale mas_makeConstraints:^(MASConstraintMaker *make) {
    
        make.left.equalTo(name.mas_left);
        make.top.equalTo(name.mas_bottom).offset(5);
        
    }];
    //点赞图片
    [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.baseline.equalTo(sale.mas_baseline);
//        make.left.equalTo(sale.mas_right).offset(5);
        make.left.equalTo(icon.mas_right).offset(50);
        make.bottom.equalTo(icon).offset(-12);
        make.size.mas_equalTo(imageName.size);
    }];
    //点赞数
    [prise mas_makeConstraints:^(MASConstraintMaker *make) {
        make.baseline.equalTo(imageV);
        make.left.equalTo(imageV.mas_right).offset(5);
    }];
    
    [price mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(sale.mas_left);
        make.top.equalTo(sale.mas_bottom).offset(5);
    }];
    
    [des mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(icon.mas_left);
        make.top.equalTo(icon.mas_bottom).offset(5);
        make.right.equalTo(self.contentView).offset(-5);
        make.bottom.equalTo(self.contentView).offset(-5);
    }];
    
    [minusView mas_makeConstraints:^(MASConstraintMaker *make) {
      
        make.baseline.equalTo(prise).offset(10);
        
        make.right.equalTo(self.contentView.mas_right).offset(-10);
        make.size.mas_equalTo(minusView.bounds.size);
        
    }];
    
    
}
- (void)setModel:(GDDetailsFoodModel *)model{
    
    _model = model;
    
    //加载图片
    NSString *urlStr = model.picture;
    //删除掉尾缀 －－ 拓展名
    urlStr = [urlStr stringByDeletingPathExtension];
    
    //重新获得URL
    NSURL *url = [NSURL URLWithString:urlStr];
    
    [_icon sd_setImageWithURL:url];
    
    _name.text = model.name;
    //月销数
    _sale.text = model.min_order_count;
    //点赞数
    _prise.text = model.praise_num.description;
//    _price.text = model.min_price.description;
    //价格
    _price.text = [NSString stringWithFormat:@"$%@",model.min_price];
    //商品详情
    _desLabel.text = model.Description;

}
- (void)minusView:(GDMinusView *)minusV withMinusNum:(NSInteger)minusNum{
    
    //通过代理将监听加减号获得的数据传到rightCell中
    //通过将监听加减按钮 获得minusNum 将其保存在foodDetailsModel.buyCount中，
    //进而将数据保存在了model中

    self.model.buyCount = minusNum;
    
    if(minusV.isPlus == YES){
        
        if([_delegate respondsToSelector:@selector(rightCell:withPlusModel:withStartP:)]){
            //在此处将加减按钮的中心点发给控制器，进行动画
            [_delegate rightCell:self withPlusModel:_model withStartP:minusV.startPoint];
        }
        
    }else{
        if([_delegate respondsToSelector:@selector(rigthCell:withMinusModel:)]){
            [_delegate rigthCell:self withMinusModel:_model];
        }
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
